package top.malegod.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import top.malegod.mapper.CommunityMapper;
import top.malegod.model.Community;
import top.malegod.service.CommunityService;
import top.malegod.utils.ServerResponse;
import top.malegod.utils.Utils;

/**
 * Create by IntelliJ IDEA
 *
 * @author: AbelEthan
 * @date-Time: 2018/5/24 15:44
 * @description:  社区实现service接口
 */
@Service
public class CommunityServiceImpl implements CommunityService {

  private static final String IMAGE_GIF = "GIF";

  private static final String IMAGE_PNG = "PNG";

  private static final String IMAGE_JPG = "JPG";

  @Autowired
  private CommunityMapper communityMapper;

  /**
   * 获取社区所有分享service层操作
   */
  @Override
  public ServerResponse findAll() {
    List<Community> communities = communityMapper.getAll();
    return ServerResponse.createSuccess(communities);
  }

  /**
   * 获取单个社区信息service层操作
   */
  @Override
  public ServerResponse findSingle(String id) {
    Community community = communityMapper.getSingle(id);
    if (community == null){
      return ServerResponse.createError("该内容已删除");
    }
    return ServerResponse.createSuccess(community);
  }

  /**
   * 获取个人所有社区service层操作
   */
  @Override
  public ServerResponse findUserCommunity(String userId) {
    List<Community> communities = communityMapper.getCommunityByUserId(userId);
    return ServerResponse.createSuccess(communities);
  }

  /**
   * /** 增加社区service层操作
   */
  @Override
  public ServerResponse addCommunity(MultipartFile file, String content, String userId,
      HttpServletRequest request) throws IOException {
    Community community = new Community();
    //判断上传的文件是否为空
    if (file != null){
      //文件路径
      String path = null;
      //文件类型
      String type = null;
      //文件原名称
      String filename = file.getOriginalFilename();
      System.out.println("上传的文件原名称：" + filename);

      //判断文件类型
      type = filename.indexOf(".") != -1 ? filename.substring(filename.lastIndexOf(".") + 1,filename.length()) : null;

      //判断文件类型是否为空
      if (type != null){
        if (IMAGE_GIF.equals(type.toUpperCase()) || IMAGE_PNG.equals(type.toUpperCase()) || IMAGE_JPG.equals(type.toUpperCase())){
          //项目在容器中实际发布运行的根路径
          String realPath = request.getServletContext().getRealPath("/upload/");
          //自定义的文件名称
          String trueFileName = String.valueOf(System.currentTimeMillis()) + filename;
          //设置存放图片文件的路径
          path = realPath + File.separator  + trueFileName;
          System.out.println("存放图片文件的路径：" + path);
          //转存文件到指定的路径
          file.transferTo(new File(path));

          community.setId(Utils.createUUID());
          community.setContent(content);
          community.setImage("upload/" + trueFileName);
          community.setUserId(userId);
          community.setCreateTime(new Date());
          int count = communityMapper.addCommunity(community);
          if (count != 1){
            return ServerResponse.createError("分享失败");
          }
          return ServerResponse.createSuccess("分享成功");
        }else {
          return ServerResponse.createError("不是我们想要的文件类型,请按要求重新上传");
        }
      }else {
        return ServerResponse.createError("文件类型为空");
      }
    }
    return ServerResponse.createError("没有找到相对应的文件");
  }


  /**
   * 删除社区service层操作
   */
  @Override
  public ServerResponse deleteCommunity(String id) {
    Community community = communityMapper.getSingle(id);
    if (community == null){
      return ServerResponse.createError("该内容已删除");
    }
    int count = communityMapper.deleteCommunityByUserIdAndId(id);
    if (count == 0){
      return ServerResponse.createError("error");
    }
    return ServerResponse.createSuccess("success");
  }

  /**
   * 社区搜索
   */
  @Override
  public ServerResponse searchCommunity(String content) {
    List<Community> communities =  communityMapper.searchCommunity(content);
    if (communities == null){
      return ServerResponse.createError("空空如也");
    }
    return ServerResponse.createSuccess(communities);
  }
}
